package as3.component.support
{
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextFormatAlign;
	
	import as3.component.Component;
	import as3.component.controls.Label;
	import as3.component.core.Dir;
	import as3.component.geom.IntRectangle;
	public class CellRenderer extends Component
	{
		
		public var data:Object;
		
		protected var _listData:ListData;
		protected var _selected:Boolean = false;
		
		public var _textField:Label;
		
		public var _upTextColor:uint;
		protected var _overTextColor:uint;
		protected var _downTextColor:uint;
		protected var _selectTextColor:uint;
		protected var _fontBold:Boolean;
		protected var _fontSize:uint;
		
		protected var _downState:DisplayObject;
		protected var _overState:DisplayObject;
		protected var _upState:DisplayObject;
		protected var _selectState:DisplayObject;
		
		protected var _simpleButton:SimpleButton;
		
		protected var labelDir:String = Dir.LEFT;
		protected var padding:Number = 0;
		
		protected var leading:uint = 0;
		
		public var defaultHeight:Number = 20;
		public function CellRenderer()
		{
			super(true);
		}
		
		override protected function initUI():void
		{
			super.initUI();
//			_uiLibrary["upImage"] = "ChatEnterIcon";
//			_uiLibrary["overImage"] = "ListBg";
//			_uiLibrary["downImage"] = "ListBg";
//			_uiLibrary["selectImage"] = "ListBg";
			
//			_uiLibrary["upImage"] = "Cell_upSkin";
//			_uiLibrary["overImage"] = "Cell_overSkin";
//			_uiLibrary["downImage"] = "Cell_downSkin";
//			_uiLibrary["selectImage"] = "Cell_selectSkin";
			_upTextColor = _defaultSkin.cellRendererTabel.upTextColor || 0xcccccc;
			_overTextColor = _defaultSkin.cellRendererTabel.overTextColor || 0xffffff;
			_downTextColor = _defaultSkin.cellRendererTabel.downTextColor || 0x999999;
			_selectTextColor = _defaultSkin.cellRendererTabel.selectTextColor || 0x000000;
			_fontBold = _defaultSkin.cellRendererTabel.fontBold || false;
			_fontSize = _defaultSkin.cellRendererTabel.fontSize || 12;
		}
		
		override protected function initCom():void
		{
			super.initCom();
			_textField = new Label();
			_textField.color = _upTextColor;
			_textField.selectable = false;
			_textField.bold = _fontBold;
			_textField.fontSize = _fontSize;
			_textField.multiline = true;
			_textField.mouseChildren = _textField.mouseEnabled = true;
			_textField.leading = leading;
			_textField.algin = TextFormatAlign.LEFT;
			_textField.width = 35;
			_textField.height = 17;
			_simpleButton = new SimpleButton();
			_simpleButton.focusRect = false;
			_simpleButton.tabEnabled = false;
			_simpleButton.useHandCursor = false;
			_simpleButton.doubleClickEnabled = true;
		}
		
		override protected function creatUI():void
		{
			super.creatUI();
			_simpleButton.upState = _upState;
			_simpleButton.downState = _downState;
			_simpleButton.overState = _overState;
			_simpleButton.hitTestState = _upState;
			_textField.text = "";
			_textField.filters = [new GlowFilter(0x000000, 1, 2, 2, 12, 1, false, false)];
			addChild(_simpleButton);
			addChild(_textField);
		}
		
		override protected function initListener():void
		{
			super.initListener();
			
			addEventListener(MouseEvent.MOUSE_OVER, $onMouseOverHandler);
			addEventListener(MouseEvent.MOUSE_UP, $onMouseUpHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, $onMouseDownHandler);
			addEventListener(MouseEvent.MOUSE_OUT, $onMouseOutHandler);
		}
		
		protected function $onMouseOverHandler(evt:MouseEvent):void
		{
			if (selected) _textField.color = _selectTextColor;
			else _textField.color = _overTextColor;
			_textField.opaqueBackground = 0x006380;
		}
		
		protected function $onMouseUpHandler(evt:MouseEvent):void
		{
			if (selected) _textField.color = _selectTextColor;
			else _textField.color = _overTextColor;
			_textField.opaqueBackground = 0x000000;
		}
		
		protected function $onMouseDownHandler(evt:MouseEvent):void
		{
			if (selected) _textField.color = _selectTextColor;
			else _textField.color = _downTextColor;
		}
		
		protected function $onMouseOutHandler(evt:MouseEvent):void
		{
			if (selected)
			{
				_textField.color = _selectTextColor;
			} 
			else
			{
				_textField.color = _upTextColor;
			} 
			_textField.opaqueBackground = 0x000000;
		}
		
		public function set downImage(dis:DisplayObject):void
		{
			if (dis && _downState != dis) 
			{
				_downState = dis;
			}
		}
		
		public function set overImage(dis:DisplayObject):void
		{
			if (dis && _overState != dis) 
			{
				_overState = dis;
			}
		}
		
		public function set upImage(dis:DisplayObject):void
		{
			if (dis && _upState != dis) 
			{
				_upState = dis;
			}
		}
		
		public function set selectImage(dis:DisplayObject):void
		{
			if (dis && _selectState != dis)
			{
				_selectState = dis;
			}
		}
		
		public function set listData(value:ListData):void
		{
			_listData = value;
			propertyChanged = true;
//			updateDisplay();
		}
		
		public function get listData():ListData
		{
			return _listData;
		}
		
		public function set selected(value:Boolean):void
		{
			if (_selected == value) return;
			_selected = value;
			if (_selected) 
			{
				if (_textField) _textField.color = _selectTextColor;
				_simpleButton.upState = _selectState;
				_simpleButton.downState = _selectState;
				_simpleButton.overState = _selectState;
				if (_selectState) _selectState.width = width;
				if (_selectState) _selectState.height = height;
			} else {
				if (_textField) _textField.color = _upTextColor;
				_simpleButton.upState = _upState;
				_simpleButton.downState = _downState;
				_simpleButton.overState = _overState;
				if (_upState) _upState.width = width;
				if (_upState) _upState.height = height;
			}
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		override public function setSize(w:Number, h:Number):void
		{
			super.setSize(w, h);
			updateCellLayout();
		}
		
		protected function updateCellLayout():void
		{
			if (_simpleButton.upState) {
				_simpleButton.upState.width = width;
				_simpleButton.upState.height = height;
			}
			if (_simpleButton.downState) {
				_simpleButton.downState.width = width;
				_simpleButton.downState.height = height;
			}
			if (_simpleButton.overState) {
				_simpleButton.overState.width = width;
				_simpleButton.overState.height = height;
			}
			
			setMaskRect(new IntRectangle(0,0,width, height));
			
			updateLabel();
			
		}
		
		protected function updateLabel():void
		{
			_textField.y = (height - _textField.height) * 0.4;
			switch(labelDir)
			{
				case Dir.LEFT:
					_textField.x = padding;
					break;
				case Dir.RIGHT:
					_textField.x = width - padding - _textField.width;
					break;
				case Dir.CENTER:
					_textField.x = (width - _textField.width) * 0.5;
					break;
			}
		}
		
		public function setLabelDir(dir:String):void
		{
			if (dir == labelDir) return;
			if (dir != Dir.LEFT && dir != Dir.RIGHT && dir != Dir.CENTER) return;
			
			labelDir = dir;
			
			updateLabel();
		}
		
		override public function updateDisplay():void
		{
			super.updateDisplay();
			if (listData) {
				_textField.htmlText = listData.label;
			} else {
				_textField.htmlText = "";
			}
			updateLabel();
		}
		
	}
}